import { Component, OnInit } from '@angular/core';
import { MessageService } from '../../../../core/service';
import { ApiService, urlService } from '../../../../core/api';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
import { NzModalService } from 'ng-zorro-antd';
import { load } from '@angular/core/src/render3';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';

@Component({
  selector: 'app-banks',
  templateUrl: './banks.component.html',
  styleUrls: ['./banks.scss']
})
export class BanksComponent implements OnInit {

  url = 'banks';
  isVisible = false;
  isConfirmLoading = false;
  validateForm: FormGroup;
  fromData:any= {
    isDefault: false
  };
  
  private _config:any;
  private _data:any;
  private _selTag:any;//当前选中
  private _altTitle:string;
  private _page:any={
    pageNo:1,
    pages:1,
    total:10,
    name:'翻页'
  } ;

  constructor(
    private msg: MessageService,
    private _pipe: TranslateService,
    private api: ApiService,
    private fb: FormBuilder,
    private modalService: NzModalService,
  ) { 
    this.validateForm = this.fb.group({
      bankAccountName: [ '', [ Validators.required ] ],
      bankName   : [ '', [ Validators.required ] ],
      bankSubBranch: [ '', [ ] ],
      bankAccountNum : [ '', [ Validators.required ] ],
      isDefault : [ this.fromData.isDefault, [] ]
    });

  };

  
  ngOnInit() {

    this.loadList();
    this.msg.sendMessage(this.url)
    this._pipe.get(['myacy-banks.add-bank-btm']).subscribe(res=>{

    })
    // test=============================================
    this._config=[
      {
        name:'默认',
        key:'isDefault',
        type:'icon'
      },{
        name:'联系人',
        key:'bankAccountName',
        class:'table-w-150-huanHang'
      },{
        name:'银行',
        key:'bankName',
        class:'table-w-150-huanHang'
      },{
        name:'支行',
        key:'bankSubBranch'
      },{
        name:'操作',
        'key':'cz',
        labels:[{
          name:'编辑',
          type:'click'
        },{
          name:'刪除',
          type:'click'
        }]
      }]
    };
          
  showModal(): void {
    this.validateForm.reset();
    this._altTitle = '新建收款银行';
    this.isVisible = true;
    this._selTag={
      name :'新增'
    };
  };

  handleOk( fromData ): void {
    this.isConfirmLoading = true;
    fromData.isDefault = fromData.isDefault ? fromData.isDefault = 'Y' : fromData.isDefault = 'N' ;
    this._selTag.name=='编辑'? this.editBank(fromData): this.addBank(fromData);
  }
  handleCancel(): void {
    this.isVisible = false;
  };
  
  onClick(e){
    this._selTag= e;
    switch (e.name) {
      case '编辑':
        e.data.isDefault = e.data.isDefault== 'Y' ?true : false ;
        this._editFrom(e.data);
      break;
      case '刪除':
        this._delFrom(e.data);
      break;
      case '翻页':
        this._page = e;
        this.loadList();
      break;
    }
  };
  
  private _editFrom(data){//編輯銀行信息
    this.validateForm.reset();
    this._altTitle = '编辑收款银行';
    this.isVisible = true;
    this.validateForm = this.fb.group({
      bankAccountName: [ data.bankAccountName, [ Validators.required ] ],
      bankName   : [ data.bankName, [ Validators.required ] ],
      bankSubBranch: [ data.bankSubBranch, [ ] ],
      bankAccountNum : [ data.bankAccountNum, [ Validators.required ] ],
      isDefault : [ data.isDefault, [] ]
    });
  };

  private _delFrom(data):void{//編輯銀行信息
    this.modalService.confirm({
      nzTitle     : '您确认需要删除此银行卡?',
      nzContent   : '<b style="color: red;">尾号为：'+ data.bankAccountNum + ' 的卡</b>',
      nzOkText    : '是',
      nzOkType    : 'danger',
      nzOnOk      : () => this.delBank(data),
      nzCancelText: '否',
      nzOnCancel  : () => console.log('Cancel')
    });
  };

  async loadList() {
    let data = await this._getAJX('银行列表',{ pageNo: this._page.pageNo });
    this.api.getData(data,false) ? this._successListOk(data.data) : '';
  };
  async delBank(tag) {
    this._page.pageNo=1;
    let data = await this._getAJX('删除银行',{bankId:tag.id});
    this.api.getData(data,{bool:true,msg:'删除成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };
  async addBank(tag) {
    let data = await this._getAJX('创建银行',tag);
    this.api.getData(data,{bool:true,msg:'创建银行成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };
  async editBank(tag) {
    this._selTag.data.bankId = this._selTag.data.id;
    let data = await this._getAJX('修改银行',Object.assign(this._selTag.data, tag));
    this.api.getData(data,{bool:true,msg:'修改银行成功！'}) ? this._successOk() : this.isVisible = this.isConfirmLoading = false;
  };

  private _successOk():void{
    this.isVisible = this.isConfirmLoading = false;
    this.loadList();
  };
  
  private _successListOk(data):void{
    this._data = data.list;
    this._page.pageNo = data.pageNo;
    this._page.pages = data.pages;
    this._page.total = this._page.pages * 10;
  };

  private  _getAJX(url:string, data:any):any{//接口交換
    let success = this.api.post(urlService('dev', url), data).toPromise();
    return success;
  };
}
